package com.guaju.yueshangclothing.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.githang.statusbar.StatusBarCompat;
import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.dao.SearchHistoryModel;
import com.guaju.yueshangclothing.databinding.ActivitySearchBinding;
import com.guaju.yueshangclothing.databinding.DialogCommonBinding;
import com.guaju.yueshangclothing.global.MyApplication;
import com.guaju.yueshangclothing.viewmodel.SearchHistoryMV;

import java.util.ArrayList;

public class SearchActivity extends AppCompatActivity {

    private ActivitySearchBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
        //一体栏
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.home_background));
        //TODO 给回车框添加点击事件，并且将搜索内容存储到数据库
        initData();
        clearSearchHistory();
    }

    private void initData() {
        //读取数据库中存储的搜索记录
        ArrayList<SearchHistoryModel> historys = MyApplication.getLiteOrm().query(SearchHistoryModel.class);
        SearchHistoryMV searchHistoryMV = new SearchHistoryMV(this, historys);
        binding.setViewmodel(searchHistoryMV);

    }

    public void clearSearchHistory() {
        binding.flClear.setOnClickListener(new View.OnClickListener() {

            private AlertDialog alertDialog;

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                DialogCommonBinding dialogCommonBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_common, null, false);
                dialogCommonBinding.dialogConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        binding.flowlayout.removeAllViews();
                        MyApplication.getLiteOrm().delete(SearchHistoryModel.class);
                        alertDialog.hide();

                    }
                });
                dialogCommonBinding.dialogCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.hide();
                    }
                });
                builder.setView(dialogCommonBinding.getRoot());
                alertDialog = builder.create();
                alertDialog.show();
            }
        });

    }


}
